#!/bin/sh

[ -z $ROX_DESKTOP ] && pidof ROX-Filer &>/dev/null && export ROX_DESKTOP=yes

if [ ! "$ROX_DESKTOP" ] ; then
	ps="`busybox ps`"
	echo "`ps`" | grep -q 'pcmanfm --desktop' && {
		pcmanfm -w "$1"
	}
	exit
fi

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"
	
if [ "$1" = "-clear" ];then
 cat $HOME/Choices/ROX-Filer/PuppyPin | grep -v '<backdrop' > /tmp/tmp
 mv -f /tmp/tmp $HOME/Choices/ROX-Filer/PuppyPin
 roxfiler -p $HOME/Choices/ROX-Filer/PuppyPin
 echo "[none]" > $HOME/.config/wallpaper/bg_img
 exit
fi

MODE="`cat $HOME/.config/wallpaper/backgroundmode`"
[ "$MODE" = "" ] && MODE="Scale"
[ "$MODE" = "Centred" ] && MODE="Centre"
[ "$MODE" = "Scaled" ] && MODE="Scale"

#w482 BK now have script that truncates an image vertically so that it has the right dimensions
#for a widescreen...
if [ -x /usr/sbin/background_reshape ];then #legacy compatibility
 if [ "$MODE" = "Stretch" ];then
  /usr/sbin/background_reshape ${1}
  type feh 2>/dev/null && feh --bg-scale $1
 fi
fi

roxfiler --RPC << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
 <env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
  <SetBackdrop>
   <Filename>$1</Filename>
   <Style>$MODE</Style>
  </SetBackdrop>
 </env:Body>
</env:Envelope>

EOF
